package com.zb.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.zb.dto.AlarmMsg;
import com.zb.dto.DeviceLocation;
import com.zb.dto.QuotaDTO;
import com.zb.emq.EmqClient;
import com.zb.service.NoticeService;
import com.zb.util.HttpUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private EmqClient emqClient;

    @Override
    public void quotaTransfer(List<QuotaDTO> quotaDTOList) {
        for (QuotaDTO quotaDTO : quotaDTOList) {
            //发送指标数据
            if (!Strings.isNullOrEmpty(quotaDTO.getWebhook())) {
                HttpUtil.httpPost(quotaDTO.getWebhook(), quotaDTO);
            }
            //有报警信息
            if ("1".equals(quotaDTO.getAlarm())
                    && !Strings.isNullOrEmpty(quotaDTO.getAlarmWebhook())) {
                String key = "device.alarm." + quotaDTO.getDeviceId() + "." + quotaDTO.getAlarmName();
                if (redisTemplate.boundValueOps(key).get() == null) {
                    HttpUtil.httpPost(quotaDTO.getAlarmWebhook(), quotaDTO);
                    //存储带有时间设置得数据
                    redisTemplate.boundValueOps(key)
                            .set(quotaDTO.getStringValue(), quotaDTO.getCycle(), TimeUnit.MINUTES);
                }
            }
            //推送前端得数据
            this.sendAlarm(quotaDTO);
        }
    }

    /**
     * 告警前端推送
     *
     * @param quotaDTO
     */
    private void sendAlarm(QuotaDTO quotaDTO) {
        if (!"1".equals(quotaDTO.getAlarm())) {
            return;
        }
        AlarmMsg alarmMsg = new AlarmMsg();
        BeanUtils.copyProperties(quotaDTO, alarmMsg);
        alarmMsg.setLevel(Integer.parseInt(quotaDTO.getLevel()));
        alarmMsg.setOnline(true);
        //发送数据
        emqClient.publish("/device/alarm", JSON.toJSONString(alarmMsg));
    }


    @Value("${webhook.online}")
    private String onlineURL;

    @Override
    public void onlineTransfer(String deviceId, Boolean online) {
        Map<String, Object> param = new HashMap<>();
        param.put("deviceId", deviceId);
        param.put("online", online);
        HttpUtil.httpPost(onlineURL, param);
    }

    @Value("${webhook.gps}")
    private String gpsURL;

    @Override
    public void gpsTransfer(DeviceLocation deviceLocation) {
        HttpUtil.httpPost(gpsURL, deviceLocation);
    }

    @Override
    public void disconnectionAlarm(String deviceId) {
        if (deviceId.startsWith("webclient")
                || deviceId.startsWith("monitor")) {
            return;
        }
        AlarmMsg alarmMsg = new AlarmMsg();
        alarmMsg.setDeviceId(deviceId);
        alarmMsg.setAlarmName("设备断网");
        alarmMsg.setLevel(1);
        alarmMsg.setOnline(false);
        alarmMsg.setAlarm(false);
        emqClient.publish("/device/alarm", JSON.toJSONString(alarmMsg));
    }
}
